package session.stateless.FRM;

import entity.CRM.Customer;
import entity.CRM.SaleOrder;
import entity.FRM.CashInAdvance;
import entity.FRM.CreditAccount;
import entity.FRM.CustomerInvoice;
import entity.FRM.CustomerPayment;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class CustomerPaymentBean implements CustomerPaymentBeanLocal {
    @PersistenceContext 
    private EntityManager em;
    
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    
    @EJB
    CashInAdvanceBeanLocal cashInAdvanceBeanLocal;
    @EJB
    CreditAccountBeanLocal creditAccountBeanLocal;
    
    @Override
    public Long createCustomerPayment_CIA(Long customerInvoiceId, Double paymentAmount) {
        CustomerInvoice customerInvoice = em.find(CustomerInvoice.class, customerInvoiceId);
        CashInAdvance cashInAdvance = customerInvoice.getCashInAdvance();
        
        CustomerPayment customerPayment = new CustomerPayment();
        Date date = new Date();
        customerPayment.setCustomerPaymentDate(dateFormat.format(date));
        customerPayment.setCustomerPaymentAmount(paymentAmount);
        customerPayment.setCustomerInvoice(customerInvoice);
        em.persist(customerPayment);
        
        cashInAdvance.setStateCashInAdvance(CashInAdvance.StateCashInAdvance.Success);
        customerInvoice.setStateCustomerInvoice(CustomerInvoice.StateCustomerInvoice.Success);
        SaleOrder saleOrder = customerInvoice.getSaleOrder();
        Customer customer = saleOrder.getCustomer();
        
        saleOrder.getCustomerPayments().add(customerPayment);
        customer.getCustomerPayments().add(customerPayment);
        creditAccountBeanLocal.modifyCreditAccount_AddCustomerPayment(customerPayment.getCustomerPaymentId());
        
        em.flush();
        
       
        
        return customerPayment.getCustomerPaymentId();
    }
    @Override
    public Long createCustomerPayment(Long customerInvoiceId, Double paymentAmount) {
        CustomerInvoice customerInvoice =em.find(CustomerInvoice.class, customerInvoiceId);
        
        CustomerPayment customerPayment = new CustomerPayment();
        Date date = new Date();
        customerPayment.setCustomerPaymentDate(dateFormat.format(date)); 
        customerPayment.setCustomerPaymentAmount(paymentAmount);
        customerPayment.setCustomerInvoice(customerInvoice);
        em.persist(customerPayment);
        
        customerInvoice.setStateCustomerInvoice(CustomerInvoice.StateCustomerInvoice.Success);
        
        SaleOrder saleOrder = customerInvoice.getSaleOrder();
        Customer customer = saleOrder.getCustomer();
        
        saleOrder.getCustomerPayments().add(customerPayment);
        customer.getCustomerPayments().add(customerPayment);
        creditAccountBeanLocal.modifyCreditAccount_AddCustomerPayment(customerPayment.getCustomerPaymentId());
        
        em.flush();
        
        return customerPayment.getCustomerPaymentId();            
    }
    
    

    @Override
    public boolean isValid_checkPaymentCIA(Long customerInvoiceId, Double paymentAmount) {
        CustomerInvoice customerInvoice = em.find(CustomerInvoice.class, customerInvoiceId);
        Double cashInAdvanceAmount = customerInvoice.getCashInAdvance().getCashInAdvanceAmount();
        if (cashInAdvanceAmount <= paymentAmount)
        {
            return true;
        }
        else 
        {
            System.out.println("Supposed payment:"+ cashInAdvanceAmount);
            return false;
        }
    }

    @Override
    public boolean isValid_checkPayment(Long customerInvoiceId, Double paymentAmount) {
        CustomerInvoice customerInvoice = em.find(CustomerInvoice.class, customerInvoiceId);
        CreditAccount creditAccount =customerInvoice.getSaleOrder().getCustomer().getCreditAccount();
        Double creditDiscount = creditAccount.getCreditDiscount();
        Long creditPeriod =creditAccount.getCreditPeriod();
        Double invoiceAmount = customerInvoice.getCustomerInvoiceAmount();
        String dateInvoice = customerInvoice.getCustomerInvoiceDate();
        
        try
        {
            //Convert the invoiceDate from String to Date
            Date now = new Date();
            Date invoiceDate = dateFormat.parse(dateInvoice);
            
            //Calculate the due for credit Period
            Calendar cal = Calendar.getInstance(); 
            cal.setTime(invoiceDate);
            cal.add(Calendar.DATE, creditPeriod.intValue());
            
            //set the date now
            Calendar cal_new = Calendar.getInstance();
            cal_new.setTime(now);
            
             if (cal.after(cal_new))  //Aka today is still within creditPeriod
             {
                 Double paymentAmount_Supposed = invoiceAmount*(1-creditDiscount/100);
                 System.out.println("supposed paymentAmount "+paymentAmount_Supposed);
                 
                 if (paymentAmount_Supposed <=paymentAmount)
                     return true;
                 else
                     return false;
             }
             else  //Aka today exceeds the creditPeriod
             {
                 if (invoiceAmount<= paymentAmount)
                     return true;
                 else return false;
             }
        }
        catch (ParseException pe)
        {
            pe.printStackTrace();
        }      
        System.out.println("Here? U gotta kidding me!");
        return true;
    }
    
    
    
    @Override
    public List<CustomerPayment> getAllCustomerPayments() {
        Query q = em.createQuery("SELECT c FROM CustomerPayment c");
        return q.getResultList();
    }

}
